/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package me.aartikov.alligator.helpers;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;


public abstract class MiFraction extends Fraction {
    IntentParams intentParams;
    int parentId;
    Object gouptag;
    boolean isNeedWaitAnimal = false;
    ShapeElement shapeElement = new ShapeElement();
    int enterAnimal = 0;
    int outAnimal = 0;

    public int getEnterAnimal() {
        return enterAnimal;
    }

    public void setEnterAnimal(int enterAnimal) {
        this.enterAnimal = enterAnimal;
    }

    public int getOutAnimal() {
        return outAnimal;
    }

    public void setOutAnimal(int outAnimal) {
        this.outAnimal = outAnimal;
    }


    public Object getGouptag() {
        return gouptag;
    }

    /**
     * 设置分组的标示
     *
     * @param gouptag 分组标示
     */
    public void setGouptag(Object gouptag) {
        if (Ability.class.isAssignableFrom(gouptag.getClass())
                || MiFraction.class.isAssignableFrom(gouptag.getClass())) {
            this.gouptag = gouptag;
        } else {
            throw new IllegalArgumentException("当前分组只能是Abilit或者Fragment的实例");
        }
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = super.onComponentAttached(scatter, container, intent);

        return component;
    }

    /**
     * 获取parentId
     *
     * @return parentId
     */
    public int getParentId() {
        return parentId;
    }

    /**
     * 设置parentId
     *
     * @param parentId 绑定parentId
     */
    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    /**
     * 获取当前的数据容器
     *
     * @return IntentParams
     */
    public IntentParams getIntentParams() {
        return intentParams;
    }

    /**
     * 设置当前的数据容器
     *
     * @param intentParams 当前页需容纳的数据
     */
    public void setIntentParams(IntentParams intentParams) {
        this.intentParams = intentParams;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        if (getComponent() != null && shapeElement != null) {
            getComponent().setBackground(shapeElement);
        }
        RunInfoStackHelp.getInstance().addFragment(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        RunInfoStackHelp.getInstance().remove(this);
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (!isNeedWaitAnimal) {
            setInitialFragmentIfRequired();
        }
    }


    protected abstract void setInitialFragmentIfRequired();

    /**
     * 是否需要等待动画执行完成
     *
     * @param needWaitAnimal 等待标示
     */
    public void setNeedWaitAnimal(boolean needWaitAnimal) {
        isNeedWaitAnimal = needWaitAnimal;
    }

    /**
     * 动画结束回调
     */
    public void animalEnd() {
        if (getComponent() != null) {
            setInitialFragmentIfRequired();
        }
    }


    /**
     * 通过资源id获取string
     *
     * @param resId 资源id
     * @return String
     */
    @Override
    public String getString(int resId) {
        return getFractionAbility().getString(resId);
    }

    /**
     * 拼接string
     *
     * @param resId      资源id
     * @param formatArgs 需要拼接的对象
     * @return 返回一个string
     */
    public String getString(int resId, Object... formatArgs) {
        return getFractionAbility().getString(resId, formatArgs);
    }
}

